package com.bestom.producetest.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;

public class ProperTiesUtils {

    @TargetApi(Build.VERSION_CODES.N)
    public static String getProperties(Activity activity, String filename, String key) {
        Properties props = new Properties();
        InputStream in = null;
        String values = null;
        try {
            //方法一：通过activity中的context攻取setting.properties的FileInputStream
            //注意这地方的参数appConfig在eclipse中应该是appConfig.properties才对,但在studio中不用写后缀
            //InputStream in = c.getAssets().open("appConfig.properties");
//            InputStream in = c.getAssets().open("appConfig");
            //方法二：通过class获取setting.properties的FileInputStream
//            InputStream in = PropertiesUtill.class.getResourceAsStream("/assets/  setting.properties "));
            //方法三：直接获取文件的properties的FileInputStream
            String basePath = null;
            if (Build.VERSION.SDK_INT >= 24) {
                basePath = activity.getFilesDir().getAbsolutePath();
            } else {
                basePath = activity.getDataDir().getAbsolutePath();
            }

            String filepath = basePath + File.separator + filename;
            in = new FileInputStream(filepath);

            props.load(in);
            values = props.getProperty(key);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return values;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static String getProperties(Context context, String filename, String key) {
        Properties props = new Properties();
        InputStream in = null;
        String values = null;
        try {
            //方法一：通过activity中的context攻取setting.properties的FileInputStream
            //注意这地方的参数appConfig在eclipse中应该是appConfig.properties才对,但在studio中不用写后缀
            //InputStream in = c.getAssets().open("appConfig.properties");
//            InputStream in = c.getAssets().open("appConfig");
            //方法二：通过class获取setting.properties的FileInputStream
//            InputStream in = PropertiesUtill.class.getResourceAsStream("/assets/  setting.properties "));
            //方法三：直接获取文件的properties的FileInputStream
            String filepath;
            if (Build.VERSION.SDK_INT >= 24) {
                filepath = context.getFilesDir().getAbsolutePath() + File.separator + filename;
            } else {
                filepath = context.getDataDir().getAbsolutePath() + File.separator + filename;
            }
            in = new FileInputStream(filepath);

            props.load(in);
            values = props.getProperty(key);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        return values;
    }

    /**
     * Get system properties
     */
    public static String getProp(String key, String defaultValue) {
        String value = defaultValue;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            value = (String) (get.invoke(c, key, "unknown"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return value;
        }
    }

    /**
     * Set system properties
     */
    public static void setProp(String key, String value) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method set = c.getMethod("set", String.class, String.class);
            set.invoke(c, key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
